// pages/appoint/appoint.js
const app = getApp();
const db = wx.cloud.database();
var util=require('../../utils/util.js');
var name = null;
var age = null;
var address = null;
var sex = null;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ne: [],
    option1: [
      { text: '全部医院', value: 0 },
      { text: '市级医院 ', value: 1 },
      { text: '县级医院', value: 2 },
      { text: '乡镇医院', value: 3 },
    ],
    option2: [
      { text: '全部科室', value: 'a' },
      { text: '内科', value: 'b' },
      { text: '外科', value: 'c' },
      { text: '妇产科', value: 'd' },
      { text: '儿童科', value: 'e' },
      { text: '其他科室', value: 'c' },
    ],
    value1: 0,
    value2: 'a',
    completed: app.globalData.completed,
    status: app.globalData.status,
    token: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    that.setData({
      status: app.globalData.status,
    })
    if(app.globalData.status==1){
      that.setData({
        completed: app.globalData.completed,
      })
      let _ = db.command;
      db.collection('user').where({
        isappointed: _.gt(0)
      }).get({
        success: res=>{
          that.setData({
            ne: res.data,
          })
        }
      });
    }else if(app.globalData.status==2){
      db.collection('appointment').where({
        doc_openid: app.globalData.token,
      }).get({
        success: res=>{
          that.setData({
            ne: res.data,
          })
        }
      });
    }
    
  },
  appoint(event){
    app.globalData.completed = true;
    var that = this;
    that.token = app.globalData.token;
    db.collection('user').where({
      _openid: that.token,
    }).get({
      success: res=>{
        console.log(res.data[0]);
        name = res.data[0].name,  
        age = res.data[0].age,
        address = res.data[0].address,
        sex = res.data[0].sex
      }
    });
    console.log("test"+name);
    db.collection('appointment').add({
      data: {
        time: util.formatTime(new Date()),
        doc_openid: event.target.id,
        name: name,
        address: address,
        sex: sex,
        age: age,
      },
      success: function(res) {
        wx.showToast({
          title: '已成功预约',
          icon:"success",
          duration:2000,
          //提示2秒后自动跳转到首页
          success:function(){
            setTimeout(function(){
              wx.switchTab({
              url: '/pages/index/index',
              })
            },2000)
          }
        })
      }
    });
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})